package com.dongjiantong.common.utils;/**
 * Created by cao on 2017/12/14.
 */

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;

/**
 * @author cao
 * @create 2017-12-14 下午3:45
 **/
@Component
public class WebUtil {


    public static HttpServletRequest getRequest(){
        ServletRequestAttributes ra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =  ra.getRequest();
        return request;
    }
    //取IP地址
    public static String hostIp()
    {
        String hostIp;
        try {
           // hostIp = InetAddress.getLocalHost().getHostAddress();
            hostIp =  clientIp();
        } //catch (UnknownHostException e)
        catch (Exception e){
            hostIp = UUID.randomUUID().toString();
        }

        return hostIp;
    }
    //取IP地址
    public static String clientIp()
    {
        HttpServletRequest request=getRequest();
        String ip ="";
        try {
            ip = request.getHeader("X-Forwarded-For");


            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("Proxy-Client-IP");

                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("WL-Proxy-Client-IP");

                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_CLIENT_IP");

                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_X_FORWARDED_FOR");

                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getRemoteAddr();
                    if (ip.equals("127.0.0.1")) {
                        // 根据网卡取本机配置的IP
                        InetAddress inet = null;
                        try {
                            inet = InetAddress.getLocalHost();
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        }
                        ip = inet.getHostAddress();
                    }

                }
            } else if (ip.length() > 15) {
                String[] ips = ip.split(",");
                for (int index = 0; index < ips.length; index++) {
                    String strIp = (String) ips[index];
                    if (!("unknown".equalsIgnoreCase(strIp))) {
                        ip = strIp;
                        break;
                    }
                }
            }
        }catch (Exception e)
        {}
        return ip;
    }



    /**
     * 浏览器名称
     * @return
     */
    public static String browserName()
    {

        //获取浏览器信息
        String ua = getRequest().getHeader("User-Agent");
        //转成UserAgent对象
        UserAgent userAgent = UserAgent.parseUserAgentString(ua);
        //获取浏览器信息
        Browser browser = userAgent.getBrowser();

        //浏览器名称
        String browserName = browser.getName();

        return browserName;
    }

    public static String  browserVer()
    {
        //获取浏览器信息
        String ua = getRequest().getHeader("User-Agent");
        //转成UserAgent对象
        UserAgent userAgent = UserAgent.parseUserAgentString(ua);
        //获取浏览器信息
        Browser browser = userAgent.getBrowser();
        String browserVer = browser.getVersion(ua).toString();

        return browserVer;
    }

    public static String generateId(String key) {
        Character SEP = 'Z';
        String hostIp = hostIp();
        String hostIpMd5 = Hashing.md5().hashString(hostIp, Charsets.UTF_8).toString().substring(0, 8);
        StringBuilder builder = new StringBuilder(30);
        String clientKey = Hashing.md5().hashString(key, Charsets.UTF_8).toString().substring(0, 8);
        builder.append(clientKey).append(SEP).append(hostIpMd5)
                .append(SEP).append(Long.toHexString(System.currentTimeMillis()))
                .append(SEP).append(UUID.randomUUID().toString().substring(0, 4));
        return builder.toString();
    }

    /**
     * 根据IP地址查出所在地
     * @param ip
     * @return
     */
    public static String getAreaByIP(String ip)
    {
        return "";
    }
}
